//
//  DIntervalViewController.swift
//  Drop
//
//  Created by hushaohua on 2018/2/7.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

class DIntervalViewController: DMoreListViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func tableRowSections() -> [[DTableRowItem]] {
        var sections:[[DTableRowItem]] = [[DTableRowItem]]();
        var section0:[DTableRowItem] = [DTableRowItem]();
        section0.append(DTableRowItem(title:"10分钟", closure: nil));
        section0.append(DTableRowItem(title:"20分钟", closure: nil));
        section0.append(DTableRowItem(title:"30分钟", closure: nil));
        section0.append(DTableRowItem(title:"40分钟", closure: nil));
        section0.append(DTableRowItem(title:"50分钟", closure: nil));
        section0.append(DTableRowItem(title:"60分钟", closure: nil));
        sections.append(section0);
        return sections;
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = super.tableView(tableView, cellForRowAt: indexPath);
        let row:DTableRowItem = self.sectionItems[indexPath.section][indexPath.row];
        let interval = Int(row.title.prefix(2))! * 60;
        cell.accessoryType = (interval == DPreferences.defaultPreferences().notificationInterval ? .checkmark : .none);
        return cell;
    }
    
    var selectInterval: (() -> Void)?;
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath);
        let row:DTableRowItem = self.sectionItems[indexPath.section][indexPath.row];
        let interval = Int(row.title.prefix(2))! * 60;
        DPreferences.defaultPreferences().notificationInterval = interval;
        self.tableView.reloadData();
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
            DDroppingManager.sharedManager().changeNotificationInterval();
            if (self.selectInterval != nil){
                self.selectInterval!();
            }
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
